// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let { OPENID, APPID, UNIONID } = wxContext
  let { groupName, groupIndex, openid } = event
  var success = false
  var message = "设置分组失败"
  var resp = {}
  const db = cloud.database()

  if (openid) {
    openid = OPENID
  }
  var updeteResp = await db.collection('user').where({
    _openid: openid
  }).update({
    data: {
      groupName: groupName,
      groupIndex: groupIndex
    }
  }).then(res => {
    success = true
    message = "设置分组成功"
  }, error => {
    success = false
    message = "设置分组失败"
  })
  return {
    success: success,
    message: message
  }
}